package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameActdays;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.ActDay;
import com.motu.monstercity.server.game.commondata.act.ActTask;
import com.motu.monstercity.server.game.commondata.shop.Shop;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.ThinkingDataManager;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.UserAct;
import com.motu.monstercity.server.game.userdata.UserActDayTask;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;

import java.util.ArrayList;
import java.util.List;

/**
 * 七天乐活动
 * @author hxl
 */
public class ActdaysHandler {
    // CMD_GAME_ACTDAYS_INFO = 80001; // 七天乐活动信息
    public static Object CMD_GAME_ACTDAYS_INFO(RequestParam clientParam) throws Exception {
        CsGameActdays.CSGameActdaysInfoRequest request = CsGameActdays.CSGameActdaysInfoRequest.parseFrom(clientParam.getData());
        UserInfo userInfo = GameUser.getUserInfo(clientParam.getUserId());
        long actId = request.getActId();
        ActDaysManager.getUserMotifActList(userInfo, false);
        UserAct userActDays = ActDaysManager.getUserMotifAct(userInfo.getId(), actId);
        if (userActDays == null) {
            return ErrorWord.NOT_DATA;
        }
//        ActDaysManager.updateTask(userInfo, ActTask.TASK_TYPE_LOGIN, 1);
//        int groupId = act.getActParamValue(); // 期数
//        ActDaysManager.doSevenDaysActEndLogic(act);
        ActDaysManager.initTask(userInfo, userActDays);
        CsGameActdays.CSGameActdaysInfoResponse.Builder response = CsGameActdays.CSGameActdaysInfoResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        List<UserActDayTask> taskList = ActDaysManager.getUserActDayTasks(userInfo.getId(), actId);
        ProtoDataUtils.updatePBUserData(pbUserData, userActDays, true);
        for (UserActDayTask userActDayTask : taskList) {
            ProtoDataUtils.updatePBUserData(pbUserData, userActDayTask, true);
        }
        return response;
    }

    // CMD_GAME_ACTDAYS_COMPLETE_TASK = 80002; // 七天乐活动完成任务
    public static Object CMD_GAME_ACTDAYS_COMPLETE_TASK(RequestParam clientParam) throws Exception {
        CsGameActdays.CSGameActdaysCompleteTaskRequest request = CsGameActdays.CSGameActdaysCompleteTaskRequest.parseFrom(clientParam.getData());
        UserInfo userInfo = GameUser.getUserInfo(clientParam.getUserId());

        long actId = request.getActId();
        ActDaysManager.getUserMotifActList(userInfo, false);
        UserAct userActDays = ActDaysManager.getUserMotifAct(userInfo.getId(), actId);
        if (userActDays == null) {
            return ErrorWord.NOT_DATA;
        }
        int taskId = request.getTaskId();
        ActTask task = ActDaysManager.getActTask(taskId);
        int taskType = ActDaysManager.getTaskId(task.getTaskType(), task.getNum2());
        UserActDayTask userActDayTask = ActDaysManager.getUserActDayTask(userInfo.getId(), actId, taskType);
        if (userActDayTask == null || userActDayTask.getCurP() < task.getNum1()) {
            return ErrorWord.INVALID_OPERATION;
        }
        JsonArray recvTaskArray = userActDayTask.getGetRewardArray();
        if (recvTaskArray.contains(taskId)) {
            return ErrorWord.REPEATED_OPERATION; // 重复领取
        }
        CsGameActdays.CSGameActdaysCompleteTaskResponse.Builder response = CsGameActdays.CSGameActdaysCompleteTaskResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        RewardManager.addReward(userInfo, task.getReward(), pbUserData, LogType.ACT_DAYS_COMPLETE_TASK, taskId);
        recvTaskArray.add(taskId);
        userActDayTask.putGetReward(recvTaskArray.toString());
        if (MainTaskManager.isResetNumByTaskType(task.getTaskType())) {
            userActDayTask.putCurP(0);// 某任务类型的进度值,是否在领取完奖励需要重置
        }
        userActDayTask.update();
        ProtoDataUtils.updatePBUserData(pbUserData, userActDayTask);
        userActDays.addIntegralScore(task.getIntegral());
        userActDays.update();
        ProtoDataUtils.updatePBUserData(pbUserData, userActDays);
        response.setScore(task.getIntegral());
        ThinkingDataManager.pushTaskFlowEvent(userInfo, 5, taskId, 2);
        return response;
    }

    // CMD_GAME_ACTDAYS_RECV_REWARD = 80003; // 七天乐活动领取阶段奖励
    public static Object CMD_GAME_ACTDAYS_RECV_REWARD(RequestParam clientParam) throws Exception {
        CsGameActdays.CSGameActdaysRecvRewardRequest request = CsGameActdays.CSGameActdaysRecvRewardRequest.parseFrom(clientParam.getData());
        UserInfo userInfo = GameUser.getUserInfo(clientParam.getUserId());
        long actId = request.getActId();
        ActDaysManager.getUserMotifActList(userInfo, false);
        UserAct userActDays = ActDaysManager.getUserMotifAct(userInfo.getId(), actId);
        if (userActDays == null) {
            return ErrorWord.NOT_DATA;
        }
        int index = request.getIndex();
        JsonArray recvRewardArray = userActDays.getIntegralRewardArray();
        if (recvRewardArray.contains(index)) {
            return ErrorWord.REPEATED_OPERATION; // 重复领奖
        }
        int groupId = userActDays.getGroupId();
        ActDay actDay = ActDaysManager.getActDaysByDay(groupId, 1); // 只有第一天配了宝箱奖励
        if (actDay == null) {
            return ErrorWord.NOT_DATA_1; // 未配置奖励
        }
        int[] integralArray = actDay.getIntegralArray();
        if (index >= integralArray.length) {
            return ErrorWord.INVALID_OPERATION;
        }
        if (integralArray[index] > userActDays.getIntegralScore()) {
            return ErrorWord.WRONG_STATUS; // 积分不够
        }
        JsonArray integralReward = actDay.getIntegralReward();
//        String indexKey = String.valueOf(index + 1);
        if (integralReward.size() < index) {
            return ErrorWord.NOT_DATA_1;
        }
        CsGameActdays.CSGameActdaysRecvRewardResponse.Builder response = CsGameActdays.CSGameActdaysRecvRewardResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        String reward = integralReward.getString(index);
        RewardManager.addReward(userInfo, reward, pbUserData, LogType.ACT_DAYS_RECV_REWARD, index);
        recvRewardArray.add(index);
        userActDays.putIntegralReward(recvRewardArray.toString());
        userActDays.update();
        ProtoDataUtils.updatePBUserData(pbUserData, userActDays);
        return response;
    }

    // CMD_GAME_ACTDAYS_BUY_GIFT = 80004; // 七天乐活动购买礼包
    public static Object CMD_GAME_ACTDAYS_BUY_GIFT(RequestParam clientParam) throws Exception {
        CsGameActdays.CSGameActdaysBuyGiftRequest request = CsGameActdays.CSGameActdaysBuyGiftRequest.parseFrom(clientParam.getData());
        UserInfo userInfo = GameUser.getUserInfo(clientParam.getUserId());
        long actId = request.getActId();
        ActDaysManager.getUserMotifActList(userInfo, false);
        UserAct userActDays = ActDaysManager.getUserMotifAct(userInfo.getId(), actId);
        if (userActDays == null) {
            return ErrorWord.NOT_DATA;
        }
        int giftId = request.getGiftId();
        int day = userActDays.getDiffDay();

        JsonArray buyGiftArray = userActDays.getBuyGiftArray();
        if (buyGiftArray.contains(giftId)) {
            return ErrorWord.REPEATED_OPERATION; // 已购买此礼包
        }

        Shop shop = ShopManager.getShop(giftId);
        if (shop == null) {
            return ErrorWord.NOT_DATA_1; // 未找到礼包配置
        }
        if (!shop.getPriceItemList().isEmpty()) { // 可能会有免费礼包
            if (!BagManager.checkNeedItemNum(userInfo, shop.getPriceItemList())) {
                return ErrorWord.ITEM_LACK; // 道具不够
            }
        }
        List<ActDay> actDayList = new ArrayList<>(ActDaysManager.getActDaysByGroupId(userActDays.getGroupId()));
//        actDayList.sort(Comparator.comparing(ActDay::getDay));
        boolean canBuyGift = false;
        for (ActDay actDay : actDayList) {
            if (actDay.getDay() > day) {
                break;
            }
            int[] giftArray = actDay.getGiftArray();
            if (Tool.isInList(giftArray, giftId)) {
                canBuyGift = true;
                break;
            }
        }
        if (!canBuyGift) {
            return ErrorWord.INVALID_OPERATION; // 未解锁礼包
        }
        CsGameActdays.CSGameActdaysBuyGiftResponse.Builder response = CsGameActdays.CSGameActdaysBuyGiftResponse.newBuilder();
        PBUserData.Builder pbUserData = response.getUserBuilder();
        if (!shop.getPriceItemList().isEmpty()) { // 可能会有免费礼包
            RewardManager.subReward(userInfo, shop.getPriceItemList(), pbUserData, LogType.ACT_DAYS_BUY_GIFT);
        }
        RewardManager.openBox(userInfo, CommonUtils.takeReawrdItemFromStr(shop.getReward()), pbUserData, LogType.ACT_DAYS_BUY_GIFT);
        buyGiftArray.add(giftId);
        userActDays.putBuyGift(buyGiftArray.toString());
        userActDays.update();
        ProtoDataUtils.updatePBUserData(pbUserData, userActDays);
        return response;
    }

}
